package org.example.maven.service.Impl;

import org.example.maven.mapper.userMapper;
import org.example.maven.pojo.user.User;
import org.example.maven.pojo.user.UserDetailsImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private userMapper userMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user=userMapper.findUser(username);
        if(user==null){
            throw new UsernameNotFoundException("用户不存在");
        }
        List<String> permissions = userMapper.getRolePermission(user.getId());
        return new UserDetailsImpl(user,permissions);
    }
}
